package com.crois.barrier.server.utils;

import com.crois.barrier.common.utils.InetUtils;
import com.crois.barrier.common.utils.UtilsAndCommons;
import com.crois.barrier.server.config.RunningConfig;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Hou Ze Yu
 * @description net 公共类
 * @date 2020/10/12
 */
public class NetUtils {

    private static String serverAddress = null;

    public static String localServer() {
        return getLocalAddress() + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort();
    }
    public static String getLocalAddress() {
        if (StringUtils.isNotBlank(serverAddress)) {
            return serverAddress;
        }

        serverAddress = InetUtils.getSelfIp();
        return serverAddress;
    }

    public static String num2ip(int ip) {
        int[] b = new int[4];
        String x = "";

        b[0] = (ip >> 24) & 0xff;
        b[1] = (ip >> 16) & 0xff;
        b[2] = (ip >> 8) & 0xff;
        b[3] = ip & 0xff;
        x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);

        return x;
    }

}
